package cn.cxj.org.controller;

import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.org.domain.Shop;
import cn.cxj.org.query.ShopQuery;
import cn.cxj.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商铺接口
 * @author 陈鑫杰
 * @date 2022年1月23日21:46:37
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "商铺接口",description = "商铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 新增或者修改 POST
     * @param shop 商铺数据
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "商铺添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "商铺添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            } else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 删除 DELETE
     * @param id 商铺id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "商铺删除",notes = "删除时需要携带的id")
    @LogAnnotation(operateType = "商铺删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id 商铺id
     * @return
     */
    @GetMapping("/{id}")
    @LogAnnotation(operateType = "基于id查询")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    /**
     * 获取所有 GET
     * @return
     */
    @GetMapping("/list")
    @LogAnnotation(operateType = "查询所有")
    public List<Shop> findAll(){
        return shopService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query 查询参数
     * @return
     */
    @PostMapping("/page")
    @LogAnnotation(operateType = "分页和高级查询")
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids 商铺id数组
     * @return
     */
    @PatchMapping
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            shopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 店铺入驻接口
     * @param shop 店铺入驻的信息
     * @return
     */
    @PostMapping("/settlement")
    @LogAnnotation(operateType = "商铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PutMapping("/auditSuccess")
    @LogAnnotation(operateType = "审核通过")
    public AjaxResult auditSuccess(@RequestBody Shop shop){
        try {
            shopService.auditSuccess(shop);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }
}
